package com.springboot.sitework.controller;

import com.springboot.bean.Code;
import com.springboot.biz.ItemBiz;
import com.springboot.biz.PipeBiz;
import com.springboot.biz.ProjectBiz;
import com.springboot.entity.Item;
import com.springboot.entity.Pipe;
import com.springboot.entity.Project;
import com.springboot.entity.User;
import com.springboot.util.CodeHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController(value = "siteworkPipeController")
@RequestMapping(value = "/sitework/pipe")
public class PipeController {

    @Resource
    private ProjectBiz projectBiz;
    @Resource
    private PipeBiz pipeBiz;
    @Resource
    private ItemBiz itemBiz;

    /**
     * 编辑管道
     */
    @RequestMapping(value = "/editinfo")
    public ModelAndView editInfo(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Pipe pipe = pipeBiz.findInfoPipe(id, user);
        if (StringUtils.isEmpty(pipe))
            return view;
        List<Item> items = itemBiz.findListItem(pipe);
        List<Code> codes = CodeHelper.findList();
        view.setViewName("sitework/editinfo");
        view.addObject("pipe", pipe);
        view.addObject("items", items);
        view.addObject("codes", codes);
        return view;
    }

    /**
     * 添加管道
     */
    @RequestMapping(value = "/insert")
    public ModelAndView insert(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (StringUtils.isEmpty(project))
            return view;
        List<Pipe> pipes = pipeBiz.findListPipe(project);
        Pipe pipe = new Pipe();
        if (pipes != null && !pipes.isEmpty()) {
            Pipe temp = pipes.get(pipes.size() - 1);
            pipe.setNo(temp.getNo() + 1);
            pipe.setWorkorder(temp.getWorkorder());
        } else
            pipe.setNo(1);
        pipe.setOperator(project.getOperator());
        pipe.setPurposes("Structural defects");
        pipe.setSlope(project.getSlope());
        if ("N".equals(project.getSlope()))
            pipe.setSloperef("N/A");
        pipe.setDate(project.getDate());
        pipe.setTime("00:00");
        pipe.setProject(project);
        id = pipeBiz.appendPipe(pipe);
        view.setViewName("redirect:editinfo?id=" + id);
        return view;
    }

    /**
     * 更新管道
     */
    @RequestMapping(value = "/update")
    public boolean update(Pipe pipe) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if (pipeBiz.findInfoPipe(pipe.getId(), user) == null)
            return true;
        pipeBiz.updatePipe(pipe);
        for (Item item : pipe.getItems()) {
            item.setPipe(pipe);
            if (item.getId() != 0) {
                itemBiz.updateItem(item);
            }
            if (item.getId() == 0 && !StringUtils.isEmpty(item.getDist()))
                itemBiz.insertItem(item);
        }
        return true;
    }

    /**
     * 删除管道
     */
    @RequestMapping(value = "/delete")
    public boolean delete(@RequestParam(defaultValue = "0") int id) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Pipe pipe = pipeBiz.findInfoPipe(id, user);
        if (!StringUtils.isEmpty(pipe))
            pipeBiz.deletePipe(pipe);
        return true;
    }

}
